<template>
    <div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" @click="exportXlsx">导出Excel</el-button>
            </div>
            <el-table :data="tableData" border class="table" header-cell-class-name="table-header">
                <el-table-column prop="user_id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="user_phone" label="手机号"></el-table-column>
                <el-table-column prop="user_name" label="用户名"></el-table-column>
                <el-table-column prop="user_email" label="邮箱"></el-table-column>
                <el-table-column prop="user_register_time" label="注册时间"></el-table-column>
            </el-table>

            <div class="pagination">
				<el-pagination
					background
					layout="total, prev, pager, next"
					:current-page="query.pageIndex"
					:page-size="query.pageSize"
					:total="pageTotal"
					@current-change="handlePageChange"
				></el-pagination>
			</div>
        </div>
    </div>
</template>

<script setup lang="ts" name="export">
import { ref , reactive} from 'vue';
import * as XLSX from 'xlsx';
import { listUserFn } from '../api/index';

// 记录表单信息
const query = reactive({
	pageIndex: 1,
	pageSize: 5
});

// 定义对象的结构
interface UserItem {
	user_id: number | string;
	user_name: string;
	user_phone: string;
	user_email: string;
	user_register_time: string;
}

const tableData = ref<UserItem[]>([]);
const pageTotal = ref(0);

// 格式化时间
const formatime = (date: Date) => {
    if (date) {
        let dt = new Date(date);
        return dt.toLocaleDateString() + " " + dt.toLocaleTimeString();
    }
    else {
        return '0000-00-00 00:00:00';
    }
}

// 获取表格数据
const getData = (page:number, pageSize:number) => {
	let token = localStorage.getItem('auto_token')
	listUserFn({page, pageSize, token}).then(res => {
		// console.log('1.0',res)
		let  { code , result } = res;
		if(code == 200) {
            let arr = result.map((item: any )=> {
                let obj = {...item};
                obj.user_create_time = formatime(item.user_create_time)
                return obj;
            })
			tableData.value = arr;
            pageTotal.value = res.data[0].total;
		}
	});
};

getData(query.pageIndex, query.pageSize);

// 分页导航
const handlePageChange = (val: number) => {
	query.pageIndex = val;
	getData(query.pageIndex, query.pageSize);
};


const list = [['ID', '手机号', '用户名', '邮箱', '注册时间']];

const exportXlsx = () => {
    tableData.value.map((item: any, i: number) => {
        const arr: any[] = [];
        arr.push(...[
            item.user_id, 
            item.user_phone, 
            item.user_name, 
            item.user_email, 
            item.user_register_time]);
        list.push(arr);
    });
    let WorkSheet = XLSX.utils.aoa_to_sheet(list);
    let new_workbook = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(new_workbook, WorkSheet, '第一页');
    XLSX.writeFile(new_workbook, `表格.xlsx`);
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #f56c6c;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
